package com.saumya.robotlegs.view
{
	import com.saumya.robotlegs.events.FileSearchEvent;
	import com.saumya.robotlegs.events.JSFLCreationEvent;
	import com.saumya.robotlegs.events.LoggerEvent;
	import com.saumya.robotlegs.events.PublishEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import mx.collections.ArrayCollection;
	
	import nl.demonsters.debugger.MonsterDebugger;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class BaseUIMediator extends Mediator
	{
		[Inject]
		public var view:BaseUI;
		
		public function BaseUIMediator()
		{
			//Does Nothing
		}
		
		public override function onRegister():void
		{
			//listen to view components
			this.eventMap.mapListener(this.view.chooseFolderBtn,MouseEvent.CLICK,onFolderSelected,MouseEvent);
			this.eventMap.mapListener(this.view.makeBatchBtn,MouseEvent.CLICK,onBatchCreationClick,MouseEvent);
			//publish
			this.eventMap.mapListener(this.view.publishSetBtn,MouseEvent.CLICK,onPublishSetSelect,MouseEvent);
			this.eventMap.mapListener(this.view.publishAllBtn,MouseEvent.CLICK,onPublishAllSelect,MouseEvent);
			//listen to context
			this.eventMap.mapListener(this.eventDispatcher,JSFLCreationEvent.MASTER_CREATION,showConfigPanel,JSFLCreationEvent);
			this.eventMap.mapListener(this.eventDispatcher,JSFLCreationEvent.COMPLETE_BATCH_CREATION,showPublishPanel,JSFLCreationEvent);
		}
		
		//event handlers
		
		private function onFolderSelected(e:MouseEvent):void
		{
			var f:File=new File();
			f.addEventListener(Event.SELECT,onUserSelection);
			f.browseForDirectory('Select Folder');
		}
		//
		private function onUserSelection(e:Event):void
		{
			var f:File=File(e.target);
			if(f.isDirectory)
			{
				var fileSearchEvent:FileSearchEvent=new FileSearchEvent(FileSearchEvent.BEGIN_SEARCH);
				fileSearchEvent.setFileReference(f);
				fileSearchEvent.setFileType(this.view.fileTypeTxt.text);
				this.dispatch(fileSearchEvent);
			}else{
				var logEvent:LoggerEvent=new LoggerEvent(LoggerEvent.UPDATE_LOG);
				logEvent.setLogMessage(' : ERROR : YOU HAVE SELECTED A file!! Select a FOLDER to search.');
				logEvent.setHilightOption(true);
				this.dispatch(logEvent);
			}
		}
		//
		private function onBatchCreationClick(e:MouseEvent):void
		{
			var evt:JSFLCreationEvent=new JSFLCreationEvent(JSFLCreationEvent.BEGIN_BATCH_CREATION);
			evt.setNumFilesInBatch(uint(this.view.numFilesTxt.text));
			this.dispatch(evt);
		}
		//-------------start publishing--------------------------------
		private function onPublishSetSelect(e:MouseEvent):void
		{
			var evt:PublishEvent=new PublishEvent(PublishEvent.PUBLISH_BATCH);
			evt.setFileToPublish(File(this.view.batchFilesList.selectedItem));
			this.dispatch(evt);
		}
		private function onPublishAllSelect(e:MouseEvent):void
		{
			var evt:PublishEvent=new PublishEvent(PublishEvent.PUBLISH_ALL);
			this.dispatch(evt);
		}
		//-------------end publishing--------------------------------
		
		//
		private function showConfigPanel(e:JSFLCreationEvent):void
		{
			this.view.configPanel.visible=true;
		}
		//
		private function showPublishPanel(e:JSFLCreationEvent):void
		{
			this.view.publishPanel.visible=true;
			//populate the dropdown list
			var allJSFLs:Array=e.getBatchFiles();
			//making the data provider for dropdownlist
			var fileCollection:ArrayCollection=new ArrayCollection(allJSFLs);
			this.view.batchFilesList.dataProvider=fileCollection;
		}
	}
}